package com.wyh.order.common.config;

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.List;

public class NacosDataSourceInit implements InitFunc {
    // nacos server ip
    private static final String remoteAddress = "115.159.5.204:8848";
    // nacos group
    private static final String groupId = "SENTINEL_GROUP";
    // nacos dataId
    private static final String dataId = "orderservice-flow-rules";

    @Override
    public void init() throws Exception {
        ReadableDataSource<String, List<FlowRule>> dataSource =
                new NacosDataSource<List<FlowRule>>(remoteAddress,groupId,dataId,
                        source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>(){}));
        FlowRuleManager.register2Property(dataSource.getProperty());
    }
}
